
var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var wxAuthFail = true; 
var txtname1 = '提现';
var txtname2 = '收支明细';
var inviteuid = 0;
Page({
  data: {
    util: util,
    tc: 0,

    bottom_nav_data:[],
    isIphone_x: false,
    nav_bgColor: '#ffffff',
    
    version: util.version,
    token: '',
    uid: 0,
    coin:0.00,
    userInfo:{},
    contact_phone: '',

    actionSheetHidden: true,
    actionSheetItems: [
      { bindtap: 'Menu1', txt: txtname1, link: 'tixian/tixian' },
      { bindtap: 'Menu2', txt: '充值', link: 'chongzhi/chongzhi' },
      { bindtap: 'Menu3', txt: txtname2, link: 'mingxi/mingxi' }
    ],

    isShow: false,

    user_jifen: 0,

    has_cdb: false,


    waithao: false,



  },
  onLoad: function(data){
    
    var that = this;
    var tc = app.get_tc();//简体繁体转换

    inviteuid = data.inviteuid ? data.inviteuid : '';
    console.log('inviteuid', inviteuid)
    if(tc == 1){
      txtname1 = '提現';
      txtname2 = '收支明細';
    }
    that.setData({
      actionSheetItems: [
        { bindtap: 'Menu1', txt: txtname1, link: 'tixian/tixian' },
        { bindtap: 'Menu2', txt: '充值', link: 'chongzhi/chongzhi' },
        { bindtap: 'Menu3', txt: txtname2, link: 'mingxi/mingxi' }
      ],
      tc: tc
    })
    //初始化底部导航
    var b_nav_data = app.getBottomNavData('mine/mine', function (data) {
      that.setData({
        bottom_nav_data: data,
        nav_bgColor: data[0].bgcolor
      });
    });
    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color,
      })
      that.resetNavColor();
    });
    //获取关于等信息
    app.getAllAbout('contact', function (res) {
      that.setData({
        contact_phone: res
      })
    });

    wx.getSystemInfo({
      success: function(res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
          screen_h: res.windowHeight,
        });
      }
    });
    var titleName = tc == 1 ? '我' : '我';
    if (util.orgid == 20510) {
      titleName = tc == 1 ? '我' : 'MINE';
    }
    wx.setNavigationBarTitle({
      title: titleName,
    })
    // that.loadGHS(1); //获取是否有门店
  },
  onShow:function(){
    this.setData({
      verifyShow: false
    })
    // var userInfo = wx.getStorageSync('userInfo');
    // this.setData({
    //   userInfo: userInfo
    // })
    this.getLoginInfo();
  },


  getJifen: function () {
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getusersource",
        "uid": that.data.uid
      },
      success: function (responseData) {
        that.setData({
          user_jifen: responseData.data.source
        })
      }
    });
  },

  getLoginInfo: function(){
    var that = this;
    app.LoginUserInfo(function (res) {
      console.log("mine login:", res);
      wxAuthFail = res == -1;//用户拒绝授权
      if (wxAuthFail) {
        return;
      }
      that.setData({
        uid: res.uid,
        token: res.token
      });
      that.getJifen();
      that.getUseingCDB();

      util.api({
        data: {
          "uid": res.uid,
          "publickey": util.publiccode,
          "ac": "userinfo",
          "tc": that.data.tc
        },
        success: function (responseData) {
          console.log(responseData.data);
          that.setData({
            userInfo: responseData.data
          });
        }
      });

      var params = {
        "uid": res.uid,
        "publickey": util.publiccode,
        "ac": "coininfo",
        "privatekey": utilMd5.hexMD5(res.uid + 'coininfo' + res.token)
      };
      console.log("余额参数：uid=" + res.uid + "; token = " + res.token, params);
      util.api({
        method: "post",
        data: params,
        success: function (responseData) {
          console.log("余额返回：", responseData);
          that.setData({
            coin: (responseData.data.coin || 0) / 100
          });
        }
      });

      util.api({
        data: {
          "publickey": util.publiccode,
          "ac": "wxcheck"
        },
        success: function (responseData) {
          that.setData({
            isShow: responseData.data.isShow
          });
        }
      });
    },inviteuid);
  },



  //重新读取 登录用户信息
  reGetUserInfo: function (e) {
    var that = this;
    console.log("reGetUserInfo: ", e);
    if (e == 1) {
      that.getLoginInfo();
    }
  },

  actionSheetTap: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  actionSheetbindchange: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindMenu1: function (e) {//提现
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
    this.goAtherPage(e);
  },
  bindMenu2: function (e) {//充值
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
    this.goAtherPage(e);
  },
  bindMenu3: function (e) {//收支明细
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
    this.goAtherPage(e);
  },

  goAtherPage: function(e){
    console.log("goAtherPage",e)
    if (wxAuthFail) {
      // app.reAuthorize(this.reGetUserInfo);
      app.goAuthorization();
    } else {
      var url = e.currentTarget.dataset.link;
      if (url.indexOf("my-youhuiquan/my-youhuiquan") > 0){
        wx.showModal({
          title: '提示',
          content: this.data.tc==1?'即將上線':'即将上线',
          showCancel: false
        })
      } else if (url.indexOf("booktime-center/booktime-center") > 0) {
        if (wx.getStorageSync('verified') == 1 || true) {
          wx.navigateTo({ url: url })
        } else {
          this.setData({
            verifyShow: 1,
            url: url
          })
        }

      } else if (url.indexOf("booktime/booktime") > 0) {
        if (wx.getStorageSync('verified') == 1 ) {
          wx.navigateTo({ url: url })
        } else {
          this.setData({
            verifyShow: 1,
            url: url
          })
        }

      } else if (url.indexOf("message-center/message-center") > 0) {
        if (wx.getStorageSync('verified') == 1) {
          wx.navigateTo({ url: url })
        } else {
          this.setData({
            verifyShow: 1,
            url: url
          })
        }

      } else if (url.indexOf("clients/clients") > 0) {
        if (wx.getStorageSync('verified') == 1) {
          wx.navigateTo({ url: url })
        } else {
          this.setData({
            verifyShow: 1,
            url: url
          })
        }

      } else if (url.indexOf("xiaohao/xiaohao") > 0) {
        if (wx.getStorageSync('verified') == 1) {
          wx.navigateTo({ url: url })
        } else {
          this.setData({
            verifyShow: 1,
            url: url
          })
        }

      } else if (url.indexOf("live_player_list/live_player_list") > 0) {
        if (wx.getStorageSync('verified') == 1) {
          wx.navigateTo({ url: url })
        } else {
          this.setData({
            verifyShow: 1,
            url: url
          })
        }

      } else if (url.indexOf("car-commerce-set/car-commerce-set") > 0) {
        if (wx.getStorageSync('verified') == 1) {
          wx.navigateTo({ url: url })
        } else {
          this.setData({
            verifyShow: 1,
            url: url
          })
        }

      } else if (url.indexOf("agent-admin/agent-admin") > 0) {
        if (wx.getStorageSync('verified') == 1) {
          wx.navigateTo({ url: url })
        } else {
          this.setData({
            verifyShow: 1,
            url: url
          })
        }
      } else if (url.indexOf("order-list/order-list?isAdmin=1") > 0) {
        if (wx.getStorageSync('verified') == 1) {
          wx.navigateTo({ url: url })
        } else {
          this.setData({
            verifyShow: 1,
            url: url
          })
        }

      } else if (url.indexOf("liuyan-center/liuyan-center") > 0) {
        if (wx.getStorageSync('verified') == 1) {
          wx.navigateTo({ url: url })
        } else {
          this.setData({
            verifyShow: 1,
            url: url
          })
        }

      } else if (url.indexOf("income/income") > 0) {
        if (wx.getStorageSync('verified') == 1) {
          wx.navigateTo({ url: url })
        } else {
          this.setData({
            verifyShow: 1,
            url: url
          })
        }
      } else if (url.indexOf("isAdmin") > 0) {
        if (wx.getStorageSync('verified') == 1) {
          wx.navigateTo({ url: url })
        } else {
          this.setData({
            verifyShow: 1,
            url: url
          })
        }

      } else {
        wx.navigateTo({ url: url })
      }
    }
  },
  goAtherPageRedirect: function(e){
      var url = e.currentTarget.dataset.link;
      wx.redirectTo({url: url})
  },

  goAther_miniProgram: function () {
    // wx.showToast({
    //   title: '到艺天下小程序',
    // })
    var that = this;
    wx.navigateToMiniProgram({
      appId: 'wx9abd62983ac25aa0',
      path: 'pages/index/index',
      extarData: {
        open: 'happy'
      },
      envVersion: 'release',
      success(res) {
        wx.showToast({
          title: that.data.tc == 1 ? '打開成功' :'打开成功',
        })
      },
      fail(res) {
        wx.showToast({
          title: that.data.tc == 1 ? '打開失敗' :'打开失败' + JSON.stringify(res),
        })
      }
    })
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);

    var path = e.detail.value.nav_path;
    if (path && path.indexOf('navto:') == 0) {
      var navPath = path.slice(6);
      wx.navigateTo({
        url: navPath,
      })
    } else {
      wx.redirectTo({ url: path });
    }
  },

  //切换语言
  changeLanguage: function(){
    var tc = this.data.tc == 1 ? -1 : 1;
    app.set_tc(tc);
    this.setData({
      tc: app.get_tc()
    });
  },


  //获取是否已持有充电宝
  getUseingCDB: function () {
    var that = this;
    var privatekey = utilMd5.hexMD5(that.data.uid + 'isusingcdb' + that.data.token);
    util.api({
      method: "POST",
      data: {
        ac: "isusingcdb",
        uid: that.data.uid,
        privatekey: privatekey
      },
      success: function (responseData) {
        var has_cdb = false;
        if (responseData.data.boxid) {  
          has_cdb = true;
        }
        that.setData({
          has_cdb: has_cdb
        });
        
      }
    });
  },

  goChongdianBao:function(){
    wx.navigateTo({ url: "/pages/chongdianbao/chongdianbao?boxid=RL1A031712000035" });
    // wx.navigateTo({ url: "/pages/chongzhi/chongzhi?coin=0.1" });
    
  },

  //页面分享
  onShareAppMessage: function () {
    var invUid = this.data.uid ? this.data.uid : '';
    // var linkcode = this.data.data.linkcode ? this.data.data.linkcode : linkcode;
    
    var path = '/pages/mine/mine?inviteuid=' + invUid;
    console.log("分享给好友：" + path);
    
    return {
      title: '',
      path: path
    };
  },

  // 读取动态 （近况）
  loadGHS: function (cpage) {
    var that = this;
    var params = {
      publickey: util.publiccode,
      ac: "getghs",
      orgid: util.orgid,
      page: cpage,
      num: 10
    }
    util.api({
      data: params,
      success: function (responseData) {
        var result = responseData.data;
        var waithao = false;
        if(result && result.lenght >0){
          waithao = true;
        }else{
          waithao = false;
        }

        that.setData({
          waithao: waithao
        });
        
      }
    });
  },
  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    });
    this.setData({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },
  jianshe: function () {
    wx.showToast({
      title: this.data.tc == 1 ? '建設中' :'建设中',
      icon: 'loading',
      duration: 5000
    })
  },

  hideVerify: function () {
    if (!this.data.verifyShow) {
      this.setData({
        verifyShow: true
      })
    } else {
      this.setData({
        verifyShow: false
      })
    }
  },

  verifySubmit: function (e) {
    console.log('verify:', e)

    var url = e.currentTarget.dataset.url;
    if (e.detail.value.pwd == '3304') {
      wx.setStorageSync('verified', 1)
      wx.showToast({
        icon: 'success',
        title: this.data.tc == 1 ? '驗證通過' : '验证通过',
      })
      wx.navigateTo({ url: url })
    } else if (e.detail.value.pwd == '') {
      wx.showToast({
        icon: "none",
        title: this.data.tc == 1 ? '請輸入驗證碼' : '请输入验证码',
      })
    } else {
      wx.showToast({
        icon: "none",
        title: '验证码错误',
      })
    }


  },
  switchLang:function(){
    // if (this.data.tc == 0) {
    //   this.data.tc = 1
    // } else if (this.data.tc == 1){
    //   this.data.tc = 0
    // }
    var that=this;
    this.setData({
      tc: this.data.tc == 1 ? -1 : 1
    })
    app.set_tc(this.data.tc);
    app.getBottomNavData('mine/mine', function (data) {
      that.setData({
        bottom_nav_data: data,
        nav_bgColor: data[0].bgcolor
      });
    });
    var titleName = this.data.tc == 1 ? '我' : '我';
    if (util.orgid == 20510) {
      titleName = this.data.tc == 1 ? '我' : 'MINE';
    }
    wx.setNavigationBarTitle({
      title: titleName,
    })
    console.log('tc--------------->>>>>', app.get_tc())
  },

  makeCallPhone: function (e) {
    var phoneNum = e.currentTarget.dataset.number;
    console.log("phoneNum:" + phoneNum);
    wx.makePhoneCall({
      phoneNumber: phoneNum,
      success: function (e) {
        console.log("make call success:", JSON.stringify(e));
      },
      fail: function (e) {
        console.log("make call fail:", JSON.stringify(e));
      }
    })
  },

})